using Grpc.Core;
using Grpc.Core.Interceptors;

public class ParamInterceptor : Interceptor
{
    public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod<TRequest, TResponse> continuation)
    {
        var (success, reason) = CheckParam(request);
        if (!success)
        {
            throw new RpcException(new Status(StatusCode.InvalidArgument, reason!));
        }

        return await continuation(request, context);
    }

    (bool Success, string? Reason) CheckParam(object o)
    {
        foreach (var propertyInfo in o.GetType().GetProperties())
        {
            var v = propertyInfo.GetValue(o);
            if (v is string str && string.IsNullOrWhiteSpace(str))
            {
                return (false, $"{propertyInfo.Name} is null");
            }

            if (v == null)
            {
                return (false, $"{propertyInfo.Name} is null");
            }
        }


        return (true, null);
    }
}